package org.melanesia.converters.java;

import org.melanesia.converters.JavaToJavaConverter;

/**
 * Converts string representation of an Enum into an instance of this Enum.
 * 
 * @author marcin.kielar
 * 
 * @param <T>
 *            Enum class name
 */
public final class StringToEnumConverter<T extends Enum<T>> implements JavaToJavaConverter {

    /** The enum class to convert values to. */
    private final Class<T> enumClass;

    /**
     * Constructs new <code>StringToEnumConverter</code> for given Enum.
     * 
     * @param enumClass
     *            enum class
     */
    public StringToEnumConverter(final Class<T> enumClass) {
        this.enumClass = enumClass;
    }

    @Override
    public Object convert(final Object value) {
        return Enum.valueOf(enumClass, (String) value);
    }
};
